//package com.weeho.dongyong.utils;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.net.ssl.HostnameVerifier;
//import javax.net.ssl.SSLContext;
//import javax.net.ssl.SSLSession;
//import javax.net.ssl.X509TrustManager;
//import java.io.ByteArrayOutputStream;
//import java.io.DataOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.URL;
//import java.security.KeyManagementException;
//import java.security.NoSuchAlgorithmException;
//import java.security.NoSuchProviderException;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//import java.util.Map;
//import java.util.Set;
//
//public class HttpsUtil {
//
//
//	private static class TrustAnyTrustManager implements X509TrustManager {
//
//		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//		}
//
//		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//		}
//
//		public X509Certificate[] getAcceptedIssuers() {
//			return new X509Certificate[] {};
//		}
//	}
//
//	private static class TrustAnyHostnameVerifier implements HostnameVerifier {
//		public boolean verify(String hostname, SSLSession session) {
//			return true;
//		}
//	}
//
//
//	final static Logger logger = LoggerFactory.getLogger(HttpsUtil.class);
//
//	/**
//	 * post方式请求服务器(https协议)
//	 *
//	 * @param url
//	 *            请求地址
//	 * @param content
//	 *            参数
//	 * @param charset
//	 *            编码
//	 * @return
//	 * @throws java.security.NoSuchAlgorithmException
//	 * @throws java.security.KeyManagementException
//	 * @throws java.io.IOException
//	 */
//	public static byte[] post(String url, String content, String charset)
//			throws NoSuchAlgorithmException, KeyManagementException, IOException {
//		SSLContext sc = SSLContext.getInstance("SSL");
//		sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
//
//		URL console = new URL(url);
//		HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
//		conn.setSSLSocketFactory(sc.getSocketFactory());
//		conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
//		conn.connect();
//		DataOutputStream out = new DataOutputStream(conn.getOutputStream());
//		out.write(content.getBytes(charset));
//		// 刷新、关闭
//		out.flush();
//		out.close();
//		InputStream is = conn.getInputStream();
//		if (is != null) {
//			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//			byte[] buffer = new byte[1024];
//			int len = 0;
//			while ((len = is.read(buffer)) != -1) {
//				outStream.write(buffer, 0, len);
//			}
//			is.close();
//			return outStream.toByteArray();
//		}
//		return null;
//	}
//
//	/**
//	 * get方式请求服务器(https协议)
//	 *
//	 * @param url
//	 *            请求地址
//	 * @param charset
//	 *            编码
//	 * @return
//	 * @throws java.security.NoSuchAlgorithmException
//	 * @throws java.security.KeyManagementException
//	 * @throws java.io.IOException
//	 */
//	public static byte[] get(String url,  String charset)
//			throws NoSuchAlgorithmException, KeyManagementException, IOException {
//		SSLContext sc = SSLContext.getInstance("SSL");
//		sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
//
//		URL console = new URL(url);
//		HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
//		conn.setSSLSocketFactory(sc.getSocketFactory());
//		conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
//		conn.connect();
//		//DataOutputStream out = new DataOutputStream(conn.getOutputStream());
//		//out.write(content.getBytes(charset));
//		// 刷新、关闭
//		//out.flush();
//		//out.close();
//		InputStream is = conn.getInputStream();
//		if (is != null) {
//			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//			byte[] buffer = new byte[1024];
//			int len = 0;
//			while ((len = is.read(buffer)) != -1) {
//				outStream.write(buffer, 0, len);
//			}
//			is.close();
//			return outStream.toByteArray();
//		}
//		return null;
//	}
//
//	/**
//	 * get方式请求服务器(https协议)
//	 *
//	 * @param url
//	 *            请求地址
//	 * @param charset
//	 *            编码
//	 * @return
//	 * @throws java.security.NoSuchAlgorithmException
//	 * @throws java.security.KeyManagementException
//	 * @throws java.io.IOException
//	 */
//	public static String  getAsString(String url,  String charset)
//			throws NoSuchAlgorithmException, KeyManagementException, IOException {
//		byte [] bytes = HttpsUtil.get(url, charset);
//		logger.info("bytes is "+bytes);
//		if (null == bytes) {
//			return null;
//		}
//		String result = new String(bytes,charset);
//		logger.info(result);
//		return result;
//	}
//
//	public static void main(String [] args) throws KeyManagementException, NoSuchAlgorithmException, IOException{
//		String url = "https://api-csl.9h-sports.com/v1/user/app-config";
//		HttpsUtil.getAsString(url, "utf-8");
//	}
///****************************************测试*post方式添加header信息***********************************************/
//
//public static byte[] post0(String url, String content, String charset,Map<String,String> headers)
//		throws NoSuchAlgorithmException, KeyManagementException, IOException, NoSuchProviderException {
//	SSLContext sc = SSLContext.getInstance("SSL");
//	sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
//
//	URL console = new URL(url);
//	HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
//	conn.setSSLSocketFactory(sc.getSocketFactory());
//	conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
//	conn.setDoOutput(true);
//	conn.setDoInput(true);
//	if (headers != null && !headers.isEmpty()) {
//		Set<String> keySet = headers.keySet();
//		for (String key: keySet) {
//			String value =  headers.get(key);
//			conn.setRequestProperty(key,value);
//		}
//	}
//	conn.connect();
//
//	DataOutputStream out = new DataOutputStream(conn.getOutputStream());
//	out.write(content.getBytes(charset));
//	// 刷新、关闭
//	out.flush();
//	out.close();
//	InputStream is = conn.getInputStream();
//	if (is != null) {
//		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//		byte[] buffer = new byte[1024];
//		int len = 0;
//		while ((len = is.read(buffer)) != -1) {
//			outStream.write(buffer, 0, len);
//		}
//		is.close();
//		return outStream.toByteArray();
//	}
//	return null;
//}
///******************************************测试*get方式添加header信息*********************************************/
//
//
//public static byte[] get0(String url,  String charset,Map headers)
//		throws NoSuchAlgorithmException, KeyManagementException, IOException {
//	SSLContext sc = SSLContext.getInstance("SSL");
//	sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
//
//	URL console = new URL(url);
//	HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
//	conn.setSSLSocketFactory(sc.getSocketFactory());
//	conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
//	conn.setDoOutput(true);
//	conn.setDoInput(true);
//	if (headers != null && !headers.isEmpty()) {
//		Set<String> keySet = headers.keySet();
//		for (String key: keySet) {
//			String value =  (String)headers.get(key);
//			conn.setRequestProperty(key,value);
//		}
//	}
//	conn.connect();
//	InputStream is = conn.getInputStream();
//	if (is != null) {
//		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//		byte[] buffer = new byte[1024];
//		int len = 0;
//		while ((len = is.read(buffer)) != -1) {
//			outStream.write(buffer, 0, len);
//		}
//		is.close();
//		return outStream.toByteArray();
//	}
//	return null;
//}
//}